<?php
import( 'de.ceus-media.framework.neon.DefinitionAction' );
import( 'classes.models.RightObject' );
import( 'classes.models.RightObjectAction' );
/**
 *	Actions for Roles.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionAction
 *	@uses			RightObject
 *	@uses			RightObjectAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 */
/**
 *	Actions for Roles.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_DefinitionAction
 *	@uses			RightObject
 *	@uses			RightObjectAction
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		0.6
 */
class RightObjectActions extends CMF_Krypton_DefinitionAction
{
	public function act()
	{
		$config		= $this->ref->get( "config" );
		$request	= $this->ref->get( "request" );

		$this->loadLanguage( 'right_object' );

		if( $request->get( 'rightobject_id' ) )
		{
			if( $request->get( 'editObject' ) )
				$this->editObject();
			else if( $request->get( 'removeObject' ) )
				$this->removeObject();
			else if( $request->get( 'addActions' ) )
				$this->addActions();
			else if( $request->get( 'removeActions' ) )
				$this->removeActions();
		}
		else if( $request->get( 'addObject' ) )
			$this->addObject();
	}

	private function addActions()
	{
		$request	= $this->ref->get( 'request' );
		if( $rightobject_id = $request->get( 'rightobject_id' ) )
		{
			$relation	= new RightObjectAction();
			$actions	= (array)$request->get( 'actions' );
			foreach( $actions as $action )
			{
				$data	= array(
					"rightobject_id"	=> $rightobject_id,
					"rightaction_id"	=> $action,
				);
				$relation->addData( $data );
			}
		}
		else
			$this->messenger->noteError( $this->words['right_object']['msg']['error1'] );
	}

	private function removeActions()
	{
		$request	=& $this->ref->get( 'request' );
		if( $rightobject_id = $request->get( 'rightobject_id' ) )
		{
			$relation	= new RightObjectAction();
			$relation->focusForeign( "rightobject_id", $rightobject_id );
			$actions	= (array)$request->get( 'actions_has' );
			foreach( $actions as $action )
			{
				$relation->focusForeign( "rightaction_id", $action );
				$relation->deleteData();
			}
		}
		else
			$this->messenger->noteError( $this->words['right_object']['msg']['error1'] );
	}

	/**
	 *	Adds a Right Object.
	 *	@access		private
	 *	@return		void
	 */
	private function addObject()
	{
		$request	=& $this->ref->get( 'request' );
		if( $this->validateForm( 'right_object', 'addObject', 'right_object', 'add' ) )
		{
			$add_object = $request->get( 'add_object' );
			$object	= new RightObject();
			$objects	= $object->getAllData();
			$objects	= $objects[$object->getTableName()];
			foreach( $objects as $object )
				if( strtolower( $object['title'] ) == strtolower( $add_object ) )
					return $this->messenger->noteError( $this->words['right_object']['msg']['error3'], $add_object );
			$object	= new RightObject();
			$data	= array(
				"object"		=> $add_object,
				"description"	=> $request->get( 'add_description' ),
				);
			$roid	= $object->addData( $data );
			$request->remove( 'add_object' );
			$request->remove( 'add_description' );
			$this->messenger->noteSuccess( $this->words['right_object']['msg']['success1'], $add_object );
		}
	}

	/**
	 *	Edits a Right Object.
	 *	@access		private
	 *	@return		void
	 */
	private function editObject()
	{
		$request	=& $this->ref->get( 'request' );
		if( $rightobject_id = $request->get( 'rightobject_id' ) )
		{
			if( $this->validateForm( 'right_object', 'editObject', 'right_object', 'edit' ) )
			{
				$objectname = $request->get( 'edit_title' );
				$object	= new RightObject();
				$objects	= $object->getAllData();
				$objects	= $objects[$object->getTableName()];
				foreach( $objects as $object )
					if( $object['rightobject_id'] != $rightobject_id && strtolower( $object['title'] ) == strtolower( $objectname ) )
						return $this->messenger->noteError( $this->words['right_object']['msg']['error3'], $objectname );
				$data	= array(
					"object"		=> $objectname,
					"description"	=> $request->get( 'edit_description' ),
					);
				$object	= new RightObject( $rightobject_id );
				$object->modifyData( $data );
				$this->messenger->noteSuccess( $this->words['right_object']['msg']['success2'], $objectname );
			}
		}
		else
			$this->messenger->noteError( $this->words['right_object']['msg']['error1'] );
	}

	/**
	 *	Removes a Right Object.
	 *	@access		private
	 *	@return		void
	 *	@todo		finish Implementation
	 */
	private function removeObject()
	{
		$request	=& $this->ref->get( 'request' );
		if( $rightobject_id = $request->get( 'rightobject_id' ) )
		{
			$object	= new RightObject( $rightobject_id );
			$data	= $object->getData( false, true );
			$objectname	= $data['title'];
			$object->deleteData();
			$request->remove( 'rightobject_id' );
			$this->messenger->noteSuccess( $this->words['right_object']['msg']['success3'], $objectname );
		}
	}
}
?>